const changeMdInfo = require("../../modules/changeMdInfo");

module.exports = (req, res) => {
  if (
    !req.query.user_id ||
    !req.query.user_name ||
    !req.query.title ||
    !req.query.type ||
    !/[123]/.test(req.query.type)
  ) {
    return res.status(200).json({ msg: "参数错误", code: 400 });
  }

  if (req.query.type == 1) {
    changeMdInfo
      .changeGood(req.query)
      .then(() => {
        return res.status(200).json({ msg: "点赞成功", code: 200 });
      })
      .catch(err => {
        console.log(err);
        return res
          .status(200)
          .json({ msg: "服务器返回错误，请稍后重试", code: 500 });
      });
  } else if (req.query.type == 2) {
    changeMdInfo
      .changeRead(req.query)
      .then(() => {
        return res.status(200).json({ msg: "浏览成功", code: 200 });
      })
      .catch(err => {
        console.log(err);
        return res
          .status(200)
          .json({ msg: "服务器返回错误，请稍后再试", code: 500 });
      });
  } else if (req.query.type == 3) {
    changeMdInfo
      .changeMd(req.query)
      .then(() => {
        return res.status(200).json({ msg: "修改成功", code: 200 });
      })
      .catch(err => {
        console.log(err);
        return res
          .status(200)
          .json({ msg: "服务器返回错误，请稍后再试", code: 500 });
      });
  }
};
